import { JupiterPermission, JupiterPermissionQuery, JupiterRolePermission } from '../types/permissionTypes';
import request from 'utils/request';

export const fetchPermissionList = (params: JupiterPermissionQuery) => {
  return request.post('/permissions/page', params);
};

export const fetchPermissionById = (uuid: string) => {
  return request.get(`/permissions/${uuid}`);
};

export const createPermission = (data: Partial<JupiterPermission>) => {
  return request.post('/permissions', data);
};

export const updatePermission = (uuid: string, data: Partial<JupiterPermission>) => {
  return request.put(`/permissions/${uuid}`, data);
};

export const deletePermission = (uuid: string) => {
  return request.delete(`/permissions/${uuid}`);
};

export const restorePermission = (uuid: string) => {
  return request.post(`/permissions/restorePermission/${uuid}`, {});
};

export const updatePermissionStatus = (uuid: string, status: number) => {
  return request.put(`/permissions/${uuid}/status`, { status });
};

export const fetchPermissionTree = () => {
  return request.get('/permissions/tree');
};

export const fetchRolePermissions = (roleId: string) => {
  return request.get(`/role-permissions/role/${roleId}`);
};

export const assignPermissionsToRole = (roleId: string, permissionIds: string[]) => {
  return request.post(`/role-permissions/assign-permissions-to-role`, { roleId, permissionIds });
};

export const fetchRolePermissionList = (params: any) => {
  return request.get('/role-permissions/page', { params });
};

export const createRolePermission = (data: Partial<JupiterRolePermission>) => {
  return request.post('/role-permissions', data);
};

export const deleteRolePermission = (uuid: string) => {
  return request.delete(`/role-permissions/${uuid}`);
};

// 权限URL关联相关API
export const assignUrlsToPermission = (uuid: string, urlResourceUuids: string[]) => {
  return request.post(`/permissions/${uuid}/urls`, urlResourceUuids);
};

export const fetchPermissionUrlResources = (uuid: string) => {
  return request.get(`/permissions/${uuid}/urls`);
};
